﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace FDFtpSync.Core
{
    /*
     * CreatedOn: 2018-05-23
     * CreatedBy: Jackie Lee(天宇遊龍)
     */
    /// <summary>
    /// 上下文
    /// </summary>
    public class SyncContext
    {
        private SyncContext() { }

        public static SyncContext Current { get; } = new SyncContext();

        public ISerializer Serializer { get; private set; }

        public IContainer Container { get; private set; }

        //public ServerSyncConfig SyncConfig { get; private set; }

        //public ClientSyncConfig ClientSyncConfig { get; private set; }

        public Lazy<ILogger> Logger { get; private set; }

        private SyncConfig _syncConfig;

        public T GetSyncConfig<T>() where T : SyncConfig
        {
            return (T)_syncConfig;
        }

        public void InitServ(ISerializer serializer)
        {
            Serializer = serializer;
            Logger = new Lazy<ILogger>(() => Container.Resolve<ILogger>());
            var res = File.ReadAllText("config.json", Encoding.UTF8);
            _syncConfig = Serializer.FromJson<ServerSyncConfig>(res);
            if (_syncConfig == null)
                throw new Exception("请配置好同步服务信息");

        }

        public void InitClient(ISerializer serializer)
        {
            Serializer = serializer;
            Logger = new Lazy<ILogger>(() => Container.Resolve<ILogger>());
            var res = File.ReadAllText("config.json", Encoding.UTF8);
            _syncConfig = Serializer.FromJson<ClientSyncConfig>(res);
            if (_syncConfig == null)
                throw new Exception("请配置好同步服务信息");

        }

        public void Register(IContainer container)
        {
            Container = container;
        }
    }
}
